//	Tic-Tac-Toe
//	By: Austin Noyes

#include <iostream>
#include <string>

using namespace std;

void PrintBoard();					// This function prints the Tic-Tac-Toe board, in original and updated versions
bool IsMoveLegal(char Move);			// Checks to see if the move is legal
int TurnDecider(char PlayerChar);

char Tic1 = '1';				// These global character variables represent each place on the TTT board
char Tic2 = '2';
char Tic3 = '3';
char Tic4 = '4';
char Tic5 = '5';
char Tic6 = '6';
char Tic7 = '7';
char Tic8 = '8';
char Tic9 = '9';

int main()
{
	bool isGameOver = false;			// This boolean variable keeps the game (or function 'main' running)
	string Player1Name, Player2Name;	// These string variables grab the player's names and display them accordingly

	char PlayerMove;				// This is the player move that will be translated into the various places on the board once entered
	char PlayerMark;				// This character variable tells the compiler if it is P1 entering, == 'X' or P2 == 'O'

	cout << "\t\t\tWelcome to Tic-Tac-Toe!" << endl;
	cout << "\tIn this game, each of the two players will enter their name, then" << endl;
	cout << "\t\tPlayer 1 will be asked to enter his/her first guess." << endl;
	cout << "\tThen, the board will show up with each player's guess until the" << endl;
	cout << "\t\tthree-in-a-row is made out of the numbers!" << endl << endl;
	cout << "\t   Best of luck and may the best Tic-Tac-Toe'r win!" << endl << endl;

	cout << "Player 1, please enter your name: ";			// Gathers Player 1's name
	getline(cin, Player1Name);
	cout << endl << "Thank you, " << Player1Name << endl << endl;

	cout << "Player 2, please enter your name: ";			// Gathers Player 2's name
	getline(cin, Player2Name);
	cout << endl << "Thank you, " << Player2Name << endl << endl;

	PrintBoard();

	cout << Player1Name << ", please enter your move: ";	// Engage main game loop(?)
	cin >> PlayerMove;

	while (isGameOver == false)			// Main game loop
	{
		PlayerMark = 'X';
		if (IsMoveLegal(PlayerMove) == true)
		{
			if (PlayerMove == '1')
			{
				Tic1 = PlayerMark;
			}
			else if (PlayerMove == '2')
			{
				Tic2 = PlayerMark;
			}
			else if (PlayerMove == '3')
			{
				Tic3 = PlayerMark;
			}
			else if (PlayerMove == '4')
			{
				Tic4 = PlayerMark;
			}
			else if (PlayerMove == '5')
			{
				Tic5 = PlayerMark;
			}
			else if (PlayerMove == '6')
			{
				Tic6 = PlayerMark;
			}
			else if (PlayerMove == '7')
			{
				Tic7 = PlayerMark;
			}
			else if (PlayerMove == '8')
			{
				Tic8 = PlayerMark;
			}
			else if (PlayerMove == '9')
			{
				Tic9 = PlayerMark;
			}
			PrintBoard();
			isGameOver = true;
		}
	}

	system("PAUSE");
	return 0;
}

void PrintBoard()
{
	cout << "   " << Tic1 << "|" << Tic2 << "|" << Tic3 << endl;
	cout << "   - - -" << endl;
	cout << "   " << Tic4 << "|" << Tic5 << "|" << Tic6 << endl;
	cout << "   - - -" << endl;
	cout << "   " << Tic7 << "|" << Tic8 << "|" << Tic9 << endl << endl;
}

bool IsMoveLegal(char Move)
{
	char PlayerMove;
	PlayerMove = Move;

	if (PlayerMove == '1' || PlayerMove == '2' || PlayerMove == '3' ||
		PlayerMove == '4' || PlayerMove == '5' || PlayerMove == '6' ||
		PlayerMove == '7' || PlayerMove == '8' || PlayerMove == '9')
	{
		if (PlayerMove == '1' && Tic1 == '1')
		{
			return true;
		}
		else if (PlayerMove == '2' && Tic2 == '2')
		{
			return true;
		}
		else if (PlayerMove == '3' && Tic3 == '3')
		{
			return true;
		}
		else if (PlayerMove == '4' && Tic4 == '4')
		{
			return true;
		}
		else if (PlayerMove == '5' && Tic5 == '1')
		{
			return true;
		}
		else if (PlayerMove == '6' && Tic6 == '2')
		{
			return true;
		}
		else if (PlayerMove == '7' && Tic7 == '1')
		{
			return true;
		}
		else if (PlayerMove == '8' && Tic8 == '2')
		{
			return true;
		}
		else if (PlayerMove == '9' && Tic9 == '1')
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

int TurnDecider(char PlayerChar)
{
	return 0;
}

//	Hey Liz! So far I have written three functions (PrintBoard, IsMoveLegal, and TurnDecider. I've only made two of the three definitions for said functions
//	The problem I'm having is with the definition of int TurnDecider(char PlayerChar). The way it is supposed to work (and it's a long-winded way of
//	doing what I'm trying to do, but I want to see if it'll work) is it's a custom algorithm that computes the total 'X's and 'O's on the board and decides
//	from that if it's Player1 or Player2's turn. Again, I'm sure there are plenty of ways to do that, but it's the way I figured would be most effective.
//	So, what I would like you to do is answer my if & if...else question (nested vs. non-nested control structures) and see if you can create a way to craft
//	that algorithm or perhaps a better way for you?

//	Please take a look, create your own copy and build/delete what you need to from here. :) Thanks for all the help and please post a TicTacToe-Josh version
//	when you're done amending my current code (or build yours from scratch.) Thanks and I'm excited to here from you.